//
//  WHMineController.m
//  SquirrelCourier
//
//  Created by 邓伟浩 on 2019/1/3.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHMineController.h"
#import "WHMenuTipsCell.h"
#import "WHMineInfoCell.h"
#import "WHCoopShopController.h" // 合作门店
#import "WHMyWalletController.h" // 我的钱包
#import "WHResetPasswordController.h" // 修改密码
#import "WHAboutUsController.h" // 关于我们


@interface WHMineController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *dataTableView;

@property (nonatomic, strong) NSArray *tipsImageArray;
@property (nonatomic, strong) NSArray *tipsNameArray;

@end

@implementation WHMineController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的";
    [self initParams];
    [self setUpTableView];
}

- (void)initParams {
    _tipsImageArray = @[@[@"mine_coop_icon", @"mine_wallet_icon"], @[@"mine_resetPassword_icon", @"mine_aboutUs_icon"]];
    _tipsNameArray = @[@[@"合作门店", @"我的钱包"], @[@"修改密码", @"关于我们"]];
}

- (void)setUpTableView {
    UITableView *dataTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    dataTableView.delegate = self;
    dataTableView.dataSource = self;
    [self.view addSubview:dataTableView];
    
    [dataTableView registerClass:[WHMenuTipsCell class] forCellReuseIdentifier:NSStringFromClass([WHMenuTipsCell class])];
    [dataTableView registerClass:[WHMineInfoCell class] forCellReuseIdentifier:NSStringFromClass([WHMineInfoCell class])];
    
    dataTableView.tableFooterView = [self tableFootView];
    dataTableView.backgroundColor = CViewBgMainColor;
    dataTableView.separatorColor = CLineColor;
    dataTableView.separatorInset = UIEdgeInsetsMake(0, KNormalSpace, 0, -KNormalSpace);
    
    [dataTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.top.equalTo(@kTopHeight);
    }];
    _dataTableView = dataTableView;
}

- (UIView *)tableFootView {
    UIView *tableFootView = [[UIView alloc] init];
    tableFootView.frame = CGRectMake(0, 0, KScreenWidth, kRealwidth(75));
    
    UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    logoutButton.frame = CGRectMake(kRealwidth(32), kRealwidth(10), KScreenWidth-2*kRealwidth(32), kRealwidth(45));
    ViewRadius(logoutButton, kRealwidth(45)/2);
    [tableFootView addSubview:logoutButton];
    [logoutButton addTarget:self action:@selector(logoutClicked:) forControlEvents:UIControlEventTouchUpInside];
    NSString *logoutTitle = @"退出登录";
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:logoutTitle attributes:@{NSFontAttributeName:SYSTEMFONT(kRealwidth(17)), NSForegroundColorAttributeName:KWhiteColor}];
    [logoutButton setAttributedTitle:attributeStr forState:UIControlStateNormal];
    logoutButton.backgroundColor = CMDarkOrangeColor;
    
    return tableFootView;
}

#pragma mark - —————— UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _tipsNameArray.count+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        NSArray *rowArray = _tipsNameArray[section-1];
        return rowArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        WHMineInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WHMineInfoCell class]) forIndexPath:indexPath];
        cell.phoneNumberLabel.text = userManager.detailUserInfo.mobile;
        cell.nameLabel.text = userManager.detailUserInfo.cardName;
        cell.typeLabel.text = userManager.detailUserInfo.expressCompany;
        [cell.headImageView setImageWithURL:[NSURL URLWithString:userManager.detailUserInfo.avatar] placeholder:IMAGE_NAMED(@"mine_defaultHeader")];
        
        return cell;
    } else {
        WHMenuTipsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WHMenuTipsCell class]) forIndexPath:indexPath];
        
        NSString *tipName = _tipsNameArray[indexPath.section-1][indexPath.row];
        cell.tipsImageView.image = IMAGE_NAMED(_tipsImageArray[indexPath.section-1][indexPath.row]);
        cell.tipsLabel.text = tipName;
        
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kRealwidth(10);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
    } else {
        NSString *tipName = _tipsNameArray[indexPath.section-1][indexPath.row];
        if ([tipName isEqualToString:@"合作门店"]) {
            WHCoopShopController *coopShop = [[WHCoopShopController alloc] init];
            [self.navigationController pushViewController:coopShop animated:YES];
        }
        if ([tipName isEqualToString:@"我的钱包"]) {
            WHMyWalletController *myWallet = [[WHMyWalletController alloc] init];
            [self.navigationController pushViewController:myWallet animated:YES];
        }
        if ([tipName isEqualToString:@"修改密码"]) {
            WHResetPasswordController *resetPassword = [[WHResetPasswordController alloc] init];
            [self.navigationController pushViewController:resetPassword animated:YES];
        }
        if ([tipName isEqualToString:@"关于我们"]) {
            WHAboutUsController *aboutUs = [[WHAboutUsController alloc] init];
            [self.navigationController pushViewController:aboutUs animated:YES];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return kRealwidth(75);
    } else {
        return kRealwidth(55);
    }
}

#pragma mark - —————— 事件 ——————
- (void)logoutClicked:(UIButton *)button {
    [userManager logout:nil];
}

@end
